<?php defined('SYSPATH') or die('No direct script access.');
 
class format extends format_Core {

    /**
     * Takes a date in ISO format YYYY-MM-DD and returns localized format (e.g. D.M.YYYY).
     */
    public static function date($date) {
    	$format = Kohana::lang('dateTimeFormat.dateFormat');
        if (is_numeric($date)) {
            $timestamp = intVal($date);
        }
        else if ($date instanceof DateTime) {
        	return $date->format($format);
        }
        else {
            if (! valid::isIsoDate($date)) {
                return $date;
            }
            $timestamp = strtotime($date);
        }
        return date($format, $timestamp);
    }
    
    public static function isoDate($date) {
    	$format = 'Y-m-d';
    	if (is_numeric($date)) {
    		$timestamp = intVal($date);
    	}
    	else if ($date instanceof DateTime) {
    		return $date->format($format);
    	}
    	else {
    		if (! valid::isIsoDate($date)) {
    			return $date;
    		}
    		$timestamp = strtotime($date);
    	}
    	return date($format, $timestamp);
    }
    
    /**
     * @return year of the given date.
     */
    public static function year($date) {
        if (is_numeric($date)) {
            $timestamp = intVal($date);
        }
        else {
            if (! valid::isIsoDate($date)) {
                return $date;
            }
            $timestamp = strtotime($date);
        }
        return date('Y', $timestamp);
    }

    /**
     * @param <type> $timestamp UNIX timestamp (integer value)
     */
    public static function timestamp($timestamp) {
        if ($timestamp) {
            return self::dateAndTime($timestamp, date('G:i', $timestamp));
        }
        return '';
    }

    public static function dateAndTime($date, $time) {
        // If the time includes seconds, remove them
        return format::date($date) . ' ' . format::time($time);
    }

    public static function time($time) {
        if (trim($time) == '') {
            return '';
        }
        // If the time includes seconds, remove them
        if (is_numeric($date)) {
            $timestamp = intVal($date);
        }
        else {
            $timestamp = strtotime($time);
        }
        return date(Kohana::lang('dateTimeFormat.timeFormat'), $timestamp);
    }

    public static function boolean($value) {
        if (is_bool($value)) {
            if ($value) {
                return text::get('application.boolean-yes');
            }
            else {
                return text::get('application.boolean-no');
            }
        }
        if ("$value" === '0') {
            return text::get('application.boolean-no');
        }
        if ("$value" === '1') {
            return text::get('application.boolean-yes');
        }
        return $value;
    }

    public static function twoValueBoolean($value) {
        if ($value) {
            return text::get('application.boolean-yes');
        }
        return text::get('application.boolean-no');
    }

    public static function longText($value) {
        return '<div class="longText">' . nl2br(html::specialchars($value)) . '</div>';
    }

    public static function decimal($value) {
        if (preg_match('/^[0-9]+\\.[0]+$/', $value)) {
            $value = preg_replace('/\\.[0]+/', '', $value);
        }
        return number_format($value, 0, '.', ' ');
    }
    
    public static function price($value, $currencyCode) {
        return self::decimal($value) . ' ' . text::get('currency.mark-' . $currencyCode);
    }

    public static function address($person, $defaultCountryCode=NULL) {
        $address = '';
        if ($person['street']) {
            $address .= $person['street'];
            if ($person['postal_code'] || $person['city']) {
                $address .= ',';
            }
        }

        if ($person['postal_code']) {
            $address .= ' ' . $person['postal_code'];
        }
        if ($person['city']) {
        	if ($person['postal_code']) {
        		$address .= ',';
        	}
            $address .= ' ' . $person['city'];
        }

        if (! $defaultCountryCode || $person['country_code'] != $defaultCountryCode) {
            $address .= ', ' . $person['country_name'];
        }
        return $address;
    }

    public static function personFullName($person, $withTitle=FALSE) {
        $fullName = $person['first_name'];
        if ($person['other_names']) {
            $fullName .= ' ' . $person['other_names'];
        }
        $fullName .= ' ' . $person['last_name'];
        if ($withTitle && $person['title']) {
            $fullName = $person['title'] . ' ' . $fullName;
        }
        return $fullName;
    }

    public static function maskEmail($email) {
        return preg_replace('/.*(@.*)/', '...\1', $email);
    }
    
    public static function dateInterval($firstDate, $secondDate) {
        return self::date($firstDate) . ' - ' . self::date($secondDate);
    }

    public static function dayOfWeek($timestamp) {
        return text::get('application.dayOfWeek-' . date('N', $timestamp));
    }

    public static function monthAndDay($date) {
        if (is_numeric($date)) {
            $timestamp = intVal($date);
        }
        else {
            if (! valid::isIsoDate($date)) {
                return $date;
            }
            $timestamp = strtotime($date);
        }
        return date(Kohana::lang('dateTimeFormat.monthAndDayFormat'), $timestamp);
    }
    
    public static function monthAndDayInterval($firstDate, $secondDate) {
        return self::monthAndDay($firstDate) . ' - ' . self::monthAndDay($secondDate);
    }
    
    public static function dateInServerTimezone($timestamp) {
    	$timezone = DateTime::getTimezone();
    	$timezone.getOffset();
    	$dateTime = new DateTime($timestamp);
    }
}
